/**
 * 整理 style 棧 和 分號 轉換成 jsonData
 *
 * @param {string|Element} styleEl style 元素标签 或 style 标签内容
 * @returns
 */
function StackStyle(styleEl) {
  //   console.log(styleEl);
  let stackJson = {};

  //特殊 层级 name 层级
  let secureStatus = [];

  //层级深度 class || id || keyframes
  let rangeDeepth = ['selector'];

  //剝離字符串
  let spliteStr = '';

  //   // (*+?) { || (*+?) :
  //   let currentName = '';

  //   // :(*+?);
  //   let currentValue = '';

  //   let styleStr = toStringTypeCheck(styleEl, 'string')
  //     ? styleEl
  //     : styleEl.innerHTML;
  let styleStr;
  if (styleEl.nodeType == 1 && styleEl.nodeName == 'STYLE') {
    styleStr = styleEl.innerHTML;
  } else {
    styleStr = styleEl;
  }

  let index = 0;
  do {
    checkValue(styleStr[index]);
  } while (index++ <= styleStr.length);

  function checkValue(v) {
    //特定字段["{", ":", ";", "}"]
    switch (v) {
      case '.':
        if (rangeDeepth[0] == 'selector') {
          if (rangeDeepth[1] == undefined) {
            rangeDeepth.push('Class');
          }
          spliteStr += v;
        } else if (rangeDeepth[0] == 'detail') {
          spliteStr += v;
        }
        break;
      case '#':
        if (rangeDeepth[0] == 'selector') {
          if (rangeDeepth[1] == undefined) {
            rangeDeepth.push('ID');
          }
          spliteStr += v;
        } else if (rangeDeepth[0] == 'detail') {
          //可能 是 color 字符 引起 -- 因此关闭此提示
          //   console.log('we got an error with out KNOW!!');
          spliteStr += v;
        }
        break;
      case '@':
        if (rangeDeepth[0] == 'selector') {
          if (rangeDeepth[1] == undefined) {
            rangeDeepth.push('Keyframes');
          }
          spliteStr += v;
        } else if (rangeDeepth[0] == 'detail') {
          console.log('we got an error with out KNOW!!');
          spliteStr += v;
        }
        break;
      case '{':
        if (rangeDeepth[0] == 'selector') {
          //轉換到 内容模式
          rangeDeepth[0] = 'detail';
          if (rangeDeepth[1] == 'Class') {
            if (rangeDeepth[2] == undefined) {
              pushSpliteStr(spliteStr);
              spliteStr = '';
              rangeDeepth.push('Name');
            }
          }
          if (rangeDeepth[1] == 'ID') {
            if (rangeDeepth[2] == undefined) {
              pushSpliteStr(spliteStr);
              spliteStr = '';
              rangeDeepth.push('Name');
            }
          }
          if (rangeDeepth[1] == 'Keyframes') {
            if (rangeDeepth[2] == undefined) {
              pushSpliteStr(spliteStr);
              spliteStr = '';
              rangeDeepth.push('percent');
            }
          }
        } else if (rangeDeepth[0] == 'detail') {
          if (rangeDeepth[1] == 'Keyframes') {
            if (rangeDeepth[2] == undefined) {
              pushSpliteStr(spliteStr);
              spliteStr = '';
              rangeDeepth.push('percent');
            } else if (rangeDeepth[2] == 'percent') {
              if (rangeDeepth[3] == undefined) {
                pushSpliteStr(spliteStr);
                spliteStr = '';
                rangeDeepth.push('Name');
              }
            }
          }
        }
        break;
      case '}':
        if (rangeDeepth[0] == 'selector') {
          console.log('is not promise at Selector push "}"');
        } else if (rangeDeepth[0] == 'detail') {
          if (rangeDeepth[1] == 'Class') {
            if (rangeDeepth[2] == undefined) {
              rangeDeepth[0] = 'selector';
              rangeDeepth.pop();
              secureStatus.pop();
            }
          } else if (rangeDeepth[1] == 'ID') {
            if (rangeDeepth[2] == undefined) {
              rangeDeepth[0] = 'selector';
              rangeDeepth.pop();
              secureStatus.pop();
            }
          } else if (rangeDeepth[1] == 'Keyframes') {
            if (rangeDeepth[2] == undefined) {
              rangeDeepth[0] = 'selector';
              rangeDeepth.pop();
              secureStatus.pop();
            } else if (rangeDeepth[2] == 'percent') {
              //detail Keyframes percent name -- 移除 name| value
              if (
                ['Name', 'Value'].indexOf(rangeDeepth[rangeDeepth.length - 1]) >
                -1
              ) {
                rangeDeepth.pop();
                secureStatus.pop();
              }
              //detail Keyframes percent --移除 percent
              else {
                rangeDeepth.pop();
                secureStatus.pop();
              }
            }
          }
        }
        break;
      case ':':
        if (rangeDeepth[0] == 'selector') {
          spliteStr += v;
        } else if (rangeDeepth[0] == 'detail') {
          if (rangeDeepth[rangeDeepth.length - 1] == 'Name') {
            pushSpliteStr(spliteStr);
            spliteStr = '';
            //移除 簽名模式
            rangeDeepth.pop();
            //啓用 值模式
            rangeDeepth.push('Value');
          }
          // 可能存在 value 值 中 含有 三元运算
          else if (rangeDeepth[rangeDeepth.length - 1] == 'Value') {
            spliteStr += v;
          }
        }
        break;
      case ',':
        if (rangeDeepth[0] == 'selector') {
          spliteStr += v;
        } else if (rangeDeepth[0] == 'detail') {
          spliteStr += v;
        }
        break;
      case ';':
        if (rangeDeepth[0] == 'selector') {
          console.log('we got an error with out KNOW!!');
        } else if (rangeDeepth[0] == 'detail') {
          if (rangeDeepth[rangeDeepth.length - 1] == 'Value') {
            pushStackJson(secureStatus, spliteStr);

            //本次值記錄完成
            spliteStr = '';
            //移除 值模式
            rangeDeepth.pop();
            //退出 值記錄
            secureStatus.pop();
          }
          if (rangeDeepth[1] == 'Keyframes') {
            if (rangeDeepth[2] == 'percent') {
              if (rangeDeepth[3] == undefined) {
                pushSpliteStr(spliteStr);
                spliteStr = '';
                rangeDeepth.push('Name');
              }
            }
          }
        }
        break;
      case '\n':
        break;
      case '\t':
        break;
      case ' ':
        if (rangeDeepth[0] == 'selector') {
          spliteStr += v;
        } else if (rangeDeepth[0] == 'detail') {
          spliteStr += v;
        }
        // switch (rangeDeepth[rangeDeepth.length - 1]) {
        // 	// case "general":
        // 	// 	secureStatus.push(spliteStr)
        // 	// 	break;
        // 	// case "Class":
        // 	// 	secureStatus.push(spliteStr)
        // 	// 	break;
        // 	// case "ID":
        // 	// 	secureStatus.push(spliteStr)
        // 	// 	break;
        // 	// case "Keyframes":
        // 	// 	secureStatus.push(spliteStr)
        // 	// 	break;
        // 	// case "Keyframe":
        // 	// 	secureStatus.push(spliteStr)
        // 	// 	break;
        // 	case "percent":
        // 		secureStatus.push(spliteStr)
        // 		break;
        // 	case "Name":
        // 		secureStatus.push(spliteStr)
        // 		break;
        // 		// case "Value":
        // 		// pushStackJson(secureStatus, spliteStr)
        // 		// break;
        // }
        // spliteStr = ""
        break;
      case '%':
        if (rangeDeepth[0] == 'selector') {
          console.log('we got an error with out KNOW!!');
        } else if (rangeDeepth[0] == 'detail') {
          // if (rangeDeepth[1] == "Class" || rangeDeepth[1] == "ID" || rangeDeepth[2] == "percent") {
          // if (rangeDeepth[rangeDeepth.length - 1] == "Value") {
          spliteStr += v;
          // }
        }
        break;

      default:
        if (rangeDeepth[0] == 'selector') {
          if (rangeDeepth[1] == undefined) {
            rangeDeepth.push('general');
          }
        } else if (rangeDeepth[0] == 'detail') {
          if (['Class', 'ID', 'Keyframes'].indexOf(rangeDeepth[1]) > -1) {
            if (rangeDeepth[1] == 'Keyframes') {
              if (rangeDeepth[2] == undefined) {
                // spliteStr += "_"
                rangeDeepth.push('percent');
              }
            } else if (
              ['Name', 'Value'].indexOf(rangeDeepth[rangeDeepth.length - 1]) ==
              -1
            ) {
              rangeDeepth.push('Name');
            }
          }
        }
        spliteStr += v;
        break;
    }
  }

  function pushSpliteStr(str) {
    if (str != '') {
      if (secureStatus[secureStatus.length - 1] != str) {
        // secureStatus.push(trimHD(str));
        secureStatus.push(str.trim());
      }
    }
  }

  //   function trimHD(str) {
  //     if (str != '') {
  //       if (str[0] == ' ') {
  //         return trimHD(str.slice(1));
  //       } else if (str[str.length - 1] == ' ') {
  //         return trimHD(str.slice(0, -1));
  //       } else {
  //         return str;
  //       }
  //     }
  //   }

  function pushStackJson(path, val) {
    let key = stackJson;
    for (var i = 0; i < path.length; i++) {
      if (i == path.length - 1) {
        key['$data'] == undefined
          ? (key['$data'] = [path[i]])
          : key['$data'].push(path[i]);
        key['$digital'] == undefined ? (key['$digital'] = true) : undefined;
        // key[path[i]] = trimHD(val);
        key[path[i]] = val.trim();
      } else {
        if (key[path[i]] == undefined) {
          key['$data'] == undefined
            ? (key['$data'] = [path[i]])
            : key['$data'].push(path[i]);
          key[path[i]] = {};
        }
        key = key[path[i]];
      }
    }
  }

  //   console.log(stackJson);
  return stackJson;
}
let styleStr2Json = StackStyle;
export { styleStr2Json, StackStyle };
